package com.hq.ims.data.config.init;

import com.hq.ims.data.entity.SerialNumber;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.service.BaseSerialNumberService;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.inf.serial.ISerialNumber;
import com.hq.web.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * JBP
 *
 * @email 526666031@qq.com
 */
@Slf4j
@Configuration
public class InitializationRedisData implements ApplicationRunner, Ordered {

    @Resource(name = RedisUtils.SERIAL_NUMBER_REDIS_TEMPLATE_KEY)
    private RedisTemplate<String, ISerialNumber> serialNumberRedisTemplate;

    @Resource
    private BaseSerialNumberService baseSerialNumberService;

    @Resource
    private BaseCompanyService baseCompanyService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        this.initDefaultSiteRedis();
        baseCompanyService.updateRedisAllCompany();
    }

    /**
     * 加载序号
     */
    private void initDefaultSiteRedis() {
        StringUtils.printFlag("*", "开始加载序号");
        List<SerialNumber> list = baseSerialNumberService.list();
        if (ListUtils.isExistsEle(list)) {
            list.forEach(d -> {
                log.info("加载了序号：{}", d.getUniqueKey());
                serialNumberRedisTemplate.opsForHash().put(RedisUtils.getAllSerialNumberKey(), d.getUniqueKey(), d);
            });
        }
        StringUtils.printFlag("*", "加载序号结束");
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
